﻿using System.Threading;
using Cysharp.Threading.Tasks;
using GameFramework.Request;

namespace GameLogic.UI.Prompt
{
    /// <summary>
    /// 提示语拓展方法
    /// </summary>
    public static class PromptExtensions
    {
        /// <summary>
        /// 根据异步任务的返回值查找对应提示语进行显示
        /// </summary>
        public static async UniTask<int> OpenPrompt(this UniTask<int> uniTask, CancellationToken token = default)
        {
            var code = await uniTask;
            if (token.IsCancellationRequested)
                return code;
            
            await OpenPromptRequest.OpenPrompt(code, token);
            return code;
        }

        /// <summary>
        /// 根据异步任务的返回值查找对应提示语进行显示
        /// </summary>
        public static async UniTask<T> OpenPrompt<T>(this UniTask<T> uniTask, CancellationToken token = default) where T : IResponse
        {
            var response = await uniTask;
            if (token.IsCancellationRequested)
                return response;
            
            await OpenPromptRequest.OpenPrompt(response.ErrorCode, token);
            return response;
        }
    }
}